package com.akcome.apollo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.common.utils.bean.AkResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.akcome.apollo.common.AkResultHelper;
import com.akcome.apollo.common.CommonUtils;
import com.akcome.apollo.domain.BmpAccount;
import com.akcome.apollo.domain.BmpNoticeConfig;
import com.akcome.apollo.domain.VMiningWorkers;
import com.akcome.apollo.domain.enums.YesOrNoEnum;
import com.akcome.apollo.domain.resultcode.UserResultCode;
import com.akcome.apollo.repository.BmpNoticeConfigRepository;
import com.akcome.apollo.service.BmpAccountService;
import com.akcome.apollo.service.BmpNoticeConfigService;


/**
 * 通知配置Service接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class BmpNoticeConfigServiceImpl implements BmpNoticeConfigService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BmpNoticeConfigRepository bmpNoticeConfigRepository;
	
	@Autowired
	private BmpAccountService bmpAccountService;
	
	@Autowired
	private AkResultHelper akResultHelper;
	
	@Override
	public BmpNoticeConfig save(BmpNoticeConfig bmpNoticeConfig){
		return bmpNoticeConfigRepository.save(bmpNoticeConfig);
	}
	
	@Override
	public BmpNoticeConfig findOne(Long id){
		return bmpNoticeConfigRepository.findOne(id);
	}
	
	@Override
	public BmpNoticeConfig findByAccountId(Long accountId){
		return bmpNoticeConfigRepository.findByAccountId(accountId);
	}
	
	@Override
	public AkResult saveNoticeConfig(Long c_accountId, String isEnableNotice, BmpNoticeConfig config) {
		
		if(YesOrNoEnum.YES.getCode().equals(isEnableNotice)){
			//启动状态
			//校验
			String emails = config.getEmail();
			String[] emailArray =  emails.split(",");
			String mobiles = config.getMobile();
			String[] mobileArray =  mobiles.split(",");
			if(emailArray.length==0 && mobileArray.length==0){
				//手机和邮箱为空
				return akResultHelper.buildFailAkResult(UserResultCode.MOBILE_AND_EMAIL_NULL);
			}
			
			for (String email : emailArray) {
				if(!CommonUtils.checkEmail(email)){
					//邮箱格式错误
					return akResultHelper.buildFailAkResult(UserResultCode.EMAIL_ERROR);
				}
			}
			
			
			for (String mobile : mobileArray) {
				if(mobile.indexOf("-")==-1 || mobile.indexOf("+")==-1){
					//手机格式错误
					return akResultHelper.buildFailAkResult(UserResultCode.MOBILE_ERROR);
				}
			}
			
			//用户算力变化百分比
			Double speedChangeValue = config.getSpeedChangeValue();
			if(null!=speedChangeValue){
				if(speedChangeValue.doubleValue()<0d ||speedChangeValue.doubleValue()>100d){
					return akResultHelper.buildFailAkResult(UserResultCode.SPEED_ERROR);
				}
			}
			
		}
		
		BmpAccount bmpAccount = bmpAccountService.findOne(c_accountId);
		bmpAccount.setIsEnableNotice(isEnableNotice);
		BmpNoticeConfig bmpNoticeConfig = findByAccountId(c_accountId);
		if(null!=bmpNoticeConfig){
			BeanUtils.copyProperties(config, bmpNoticeConfig,"id","accountId","createTime");
			save(bmpNoticeConfig);
		}else{
			config.setAccountId(c_accountId);
			config.setCreateTime(new Date());
			save(config);
		}
		bmpAccountService.save(bmpAccount);
		return akResultHelper.buildSuccessAkResult(null);
	}
}
